package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Brand;
import com.by.bean.Product;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.IUpdate;
import com.by.common.query.BrandQuery;
import com.by.common.query.ProductQuery;
import com.by.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Ye QS
 * @version 0.1
 * @className BrandController
 * @description
 * @date 2024/9/19 17:08
 * @since jdk11
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "品牌管理")
public class BrandController extends BaseController {
    @Autowired
    BrandService brandService;

    @ApiImplicitParam(name = "id", value = "产品id", required = true, defaultValue = "0", dataTypeClass = Integer.class)
    @ApiOperation("删除品牌")
    @DeleteMapping("/brand")
    public ResultBean delete(@ApiParam(name = "id", required = true, value = "要删除的id") @RequestParam(name = "id", required = true, defaultValue = "0") int id) {
        int delete = brandService.delete(id);
        return deleteResult(delete);
    }

    @ApiOperation("模糊搜索")
    @PostMapping("/brand/query")
    public ResultBean query(@RequestBody BrandQuery query) {
        // 将传入的 EmpQuery 对象转换成 Map 结构，便于后续处理使用。
        Map<String, Object> map = BeanUtil.beanToMap(query);

        // 调用 employeeService 中定义的方法 query 进行员工信息查询，并接收返回的分页数据。
        PageBean pageBean = brandService.query(map);

        // 构造并返回一个包含自定义消息和实际查询结果的成功响应对象。
        return success("品牌列表", pageBean);
    }

    @ApiOperation("新增品牌")
    @PostMapping("/brand")
    public ResultBean saveBrand(@RequestBody Brand brand, BindingResult result) {
        validate(result);
        int save = brandService.save(brand);
        return saveResult(save);
    }

    @ApiOperation("修改品牌")
    @PutMapping("/brand")
    public ResultBean update(@Validated(IUpdate.class) @RequestBody Brand brand, BindingResult result) {
        validate(result);
        int rs = brandService.update(brand);
        return updateResult(rs);
    }
}
